﻿using System;
using System.Collections.Generic;
using System.Text;
using ChemicalEquilibrium;

namespace NeuralModule
{
    /// <summary>
    /// 树突
    /// 分类
    ///     简单树突
    ///         功能:
    ///             传递离子
    ///             传递信息物质
    ///             整合多个输入
    ///         组成:
    ///             钾离子
    ///             钠离子
    ///             钾离子通道
    ///             钠离子通道
    ///         平衡:
    ///             离子平衡
    ///             物质平衡
    ///             
    /// </summary>
    public class Dendrite
    {
        public List<ThreadEventWait> TEWs = new List<ThreadEventWait>();

        public Reactants[] FormerIonKs, FormerIonNas;

        public Reactants IonK, IonNa;

        public Reactants ChannelK, ChannelNa, PumpNaK;

        public ReactantTransform RTK, RTNa;

        public IonicEquilibrium IEK, IENa;

        public IonicPump IPNaK;

        public Dendrite(Reactants FormerIonK, Reactants FormerIonNa)
        {
            this.FormerIonKs = new Reactants[] { FormerIonK };
            this.FormerIonNas = new Reactants[] { FormerIonNa };

            Initialize();
        }

        public Dendrite(Reactants[] FormerIonKs, Reactants[] FormerIonNas)
        {
            this.FormerIonKs = FormerIonKs;
            this.FormerIonNas = FormerIonNas;

            Initialize();
        }

        public void Initialize()
        {
            //离子
            this.IonK = new Reactants(100000, 100000);
            this.IonNa = new Reactants(100000, 100000);

            //离子通道
            this.ChannelK = new Reactants(1000, 0);
            this.ChannelNa = new Reactants(1000, 0);

            //离子泵
            this.PumpNaK = new Reactants(20, 0);

            //离子平衡系统
            this.IEK = new IonicEquilibrium(this.ChannelK.Active, this.IonK.Active, this.IonK.Inactive, 0.09f, -80, this.GetVoltage);
            this.IENa = new IonicEquilibrium(this.ChannelNa.Active, this.IonNa.Active, this.IonNa.Inactive, 0.01f, 62, this.GetVoltage);

            //离子泵平衡系统
            this.IPNaK = new IonicPump(this.PumpNaK.Active, new Queue<Reactant>[] { this.IonK.Active, this.IonNa.Active }, new Queue<Reactant>[] { this.IonK.Inactive, this.IonNa.Inactive }, new int[] { 200, -200 }, new int[] { 90000, 110000 });

            //模块间离子平衡
            this.RTK = new ReactantTransform(this.IonK, this.FormerIonKs);
            this.RTNa = new ReactantTransform(this.IonNa, this.FormerIonNas);

            this.TEWs.Add(new ThreadEventWait(100, this.IEK.Transport));
            this.TEWs.Add(new ThreadEventWait(100, this.IENa.Transport));
            this.TEWs.Add(new ThreadEventWait(100, this.IPNaK.Transport));
            this.TEWs.Add(new ThreadEventWait(500, this.RTK.Transform));
            this.TEWs.Add(new ThreadEventWait(500, this.RTNa.Transform));

        }

        public void Start()
        {
            foreach (ThreadEventWait TEW in this.TEWs) TEW.Start();
        }

        public void Abort()
        {
            foreach (ThreadEventWait TEW in this.TEWs) TEW.Abort();
        }

        float Capacitance = 0.001f;
        public int GetVoltage()
        {
            int voltage = (int)((this.IonK.Active.Count + this.IonNa.Active.Count - this.IonK.Inactive.Count - this.IonK.Inactive.Count) * this.Capacitance);
            return voltage;
        }
    }
}
